function factory(Target, options = {}) {
  const store = {getters: {}, mutations: {}, actions: {}}

  // namespaced
  if (options.namespaced) {
    store.namespaced = true
  }

  // state
  const data = new Target()
  store.state = Object.keys(data).reduce((state, key) => {
    if (data[key] !== undefined) {
      state[key] = data[key]
    }
    return state
  }, {})

  // getters
  if (Target.prototype.getters) {
    store.getters = Target.prototype.getters
  }

  // mutations
  if (Target.prototype.mutations) {
    store.mutations = Target.prototype.mutations
  }

  // actions
  if (Target.prototype.actions) {
    store.actions = Target.prototype.actions
  }

  // modules
  if (options.modules) {
    store.modules = options.modules
  }

  // plugins
  if (options.plugins) {
    store.plugins = options.plugins
  }

  return store
}

export function Store(options) {
  if (typeof options === 'function') {
    return factory(options)
  } else {
    return target => factory(target, options)
  }
}

export function Getter(target, name) {
  if (typeof target.getters !== 'object') {
    target.getters = Object.create(null)
  }
  target.getters[name] = target[name]
}

export function Mutation(target, name) {
  if (typeof target.mutations !== 'object') {
    target.mutations = Object.create(null)
  }
  target.mutations[name] = target[name]
}

export function Action(target, name) {
  if (typeof target.actions !== 'object') {
    target.actions = Object.create(null)
  }
  target.actions[name] = target[name]
}
